import { getUserRoles, checkUserIsSuperMgrMarket } from '@/utils/storage'

/**
 * @param {Array} value
 * @returns {Boolean}
 * @example
 */
export default function checkPermission(value, modifiers = {}) {
  if (value && value instanceof Array && value.length > 0) {
    const role = getUserRoles()
    const permissionRoles = value
    const hasPermission =
      modifiers.limit && checkUserIsSuperMgrMarket()
        ? false
        : permissionRoles.includes(role)

    if (!hasPermission) {
      return false
    }
    return true
  } else {
    throw new Error(
      `need roles! Like v-permission.limit="['admin','editor']" or checkPermission(['admin','editor'],{limit:true})`
    )
  }
}
